# -*- coding:utf-8 -*-
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from . import flowStepWidget
from com.mat.rpa.views.workWindow.middlePanel.directives.webAuto.directiveWidgets.gettingOpenWebPageObjectDirective import getOpenPageFlowStepWidget
import math
class InnerWidget(QWidget):
    lineNumberWidthSignal = pyqtSignal(float) #传递宽度
    def __init__(self, flowMode, parent=None):
        super(InnerWidget,self).__init__(parent)
        self.setStyleSheet("background-color:white;")
        self.setAcceptDrops(True)
        # self.stretchy_spacer_thing = QSpacerItem(10, 10, QSizePolicy.Minimum, QSizePolicy.Expanding)
        self.stretchy_spacer_thing = QWidget()
        self.stretchy_spacer_thing.setMinimumSize(600,100)
        self.parentPanel = parent
        self.flowMode = flowMode #设置是PC流程应用还是手机流程应用
        self.indexingTreeWidget = None #流程索引树
        self.innerWidgetLayout = QVBoxLayout()
        self.innerWidgetLayout.setContentsMargins(0,0,0,0)
        self.innerWidgetLayout.setSpacing(0) #设置放入layout的控件之间空间为1
        self.setLayout(self.innerWidgetLayout)
        self.lastFlowWidget = None

    def appendWidgetInLayout(self, widget):
        self.innerWidgetLayout.addWidget(widget)

    def insertWidgetAtIndexInLayout(self, index, widget):
        self.innerWidgetLayout.insertWidget(index, widget)

    def getWidgetAt(self, index):
        return self.innerWidgetLayout.itemAt(index).widget()

    # def mousePressEvent(self, e):
    #     self.clearPressedFocusSignal.emit("清除控件点击焦点")

    def setIndexingTreeWidget(self, indexingTreeWidget):
        self.indexingTreeWidget = indexingTreeWidget

    def setFlowMode(self, flowMode):
        self.flowMode = flowMode

    def separateTreeNodeInfo(self, infoText):
        infoList = infoText.strip().split("|")
        return infoList

    def dragEnterEvent(self, e):
        if e.mimeData().hasText():
            inforList = self.separateTreeNodeInfo(e.mimeData().text())
            if inforList[0] == "leaf":
                if self.lastFlowWidget != None:
                    self.lastFlowWidget.showBottomBorder()
                e.accept()
        else:
            e.ignore()

    def dragLeaveEvent(self, e):
        if self.lastFlowWidget != None:
            self.lastFlowWidget.hideBottomBorder()

    def insertWidget(self, idx, e):
        # 创建新控件
        flowPanelWidget = flowStepWidget.FlowStepWidget(idx + 1, 2, e.mimeData().text(),
                                                        self.lineNumberWidthSignal, self)
        # 插入
        self.innerWidgetLayout.insertWidget(idx, flowPanelWidget)

    def dropWidget(self, widget):
        # 首先删除最后边的占位控件
        self.innerWidgetLayout.removeWidget(self.stretchy_spacer_thing)
        # 添加控件
        self.innerWidgetLayout.addWidget(widget)
        # 在最后添加占位控件
        self.innerWidgetLayout.addWidget(self.stretchy_spacer_thing, 1)#占据所有空白空间

    def dropEvent(self, e):
        if self.lastFlowWidget != None:
            self.lastFlowWidget.hideBottomBorder()
        currentIndex = self.innerWidgetLayout.count()
        if currentIndex == 0:#起始布局管理器中没有放占位控件，所以控件数为0，而起始序号为1
            currentIndex = 1
        infoList = self.separateTreeNodeInfo(e.mimeData().text())
        # flowPanelWidget = flowStepWidget.FlowStepWidget(currentIndex, 2, infoList[2], self.lineNumberWidthSignal, self)
        flowPanelWidget = getOpenPageFlowStepWidget.GetOpenPageObjectFlowWidget(currentIndex, 2, infoList[2], self.lineNumberWidthSignal, self)
        if currentIndex != 1:
            lastDigitNumber = math.floor(math.log10(currentIndex-1))+1
            currentDigitNumber =  math.floor(math.log10(currentIndex))+1
            if currentDigitNumber > lastDigitNumber:#位数多了一位
                self.lineNumberWidthSignal.emit(flowPanelWidget.lineNumberWidth)

        flowPanelWidget.setErrorLabelVisible(False)
        flowPanelWidget.setCodePuckerVisible(False)
        self.dropWidget(flowPanelWidget)
        self.lastFlowWidget = flowPanelWidget #更新最后一个流程控件
        # 当添加一条流程步骤时，垂直滚动条自动向下移动，能跟随显示最后一条
        #pyqt5这里可能有个bug：先滚动再在QVBoxlayout中续加控件
        x = self.parentPanel.vScrollBar.maximum()
        # x = x+100 #因为总是获得插入前的滚动条最大值，所以此时的maximum值不包含新添加的流程控件
        # # 所以手动地增大maximum值，实现估计增加后的maximum值为再增加100
        # # 最后用增加后的maximum值设置滚动条的位置就可以滚动追踪最后一条流程控件
        # # 增加多一些，可以留一些空白，否则最后添加不上了
        # self.parentPanel.vScrollBar.setMaximum(x) #要让滚动条一直在最下边必须有这一条语句
        self.parentPanel.vScrollBar.setSliderPosition(x)


class FlowScrollAreaWidget(QScrollArea):
    def __init__(self, flowMode, parent=None):
        super(FlowScrollAreaWidget, self).__init__(parent)
        self.flowMode = flowMode
        self.setAcceptDrops(True)
        self.indexingTreeWidget = None
        self.vScrollBar = self.verticalScrollBar()
        self.setWidgetResizable(True) #必须加上这句，否则scroll中的widget不能铺满
        self.initUI()

    def initUI(self):
        #scrollArea仅仅类似一个窗口，只是用来帮助在有限面积中观察更大范围
        #而它自身并不会提供要展示的容器。我们需要自己在ScrollArea中放置待观察的容器，例如这里的innerWidget
        self.innerWidget = InnerWidget(self.flowMode, self)
        self.innerWidget.setObjectName("innerWidget")
        self.setWidget(self.innerWidget)

    def getInnerWidget(self):
        return self.innerWidget

    def setFlowMode(self, flowMode):
        self.flowMode = flowMode
        self.innerWidget.setFlowMode(flowMode)

    def setIndexingTreeWidget(self, indexingTreeWidget):
        self.indexingTreeWidget = indexingTreeWidget
        self.innerWidget.setIndexingTreeWidget(indexingTreeWidget)





